﻿using System;
using System.ServiceModel.Security;
using System.Threading;
using System.Windows.Forms;
using BlueMarineWindowsFormsClient.AuthenticateService;

namespace BlueMarineWindowsFormsClient
{
    public partial class frmChangePassword : Form
    {
        private static string uName = "";
        private static string pWord = "";
        private readonly Status _status;

        public frmChangePassword(string u, string p)
        {
            InitializeComponent();
            CenterToScreen();
            uName = u;
            pWord = p;
            txtUserName.Text = uName;
            txtPassword.Text = pWord;
            _status = new Status(lblStatus);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            var t = new Thread(ThreadProc);
            t.Start();
            Close();
        }

        public void ThreadProc()
        {
            Application.Run(new frmMain(uName, pWord));
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            ChangPass();
        }

        private void ChangPass()
        {
            try
            {
                var _service = new AuthenticateServiceClient("WSHttpBinding_IAuthenticateService")
                                   {
                                       ClientCredentials =
                                           {
                                               UserName = {UserName = txtUserName.Text, Password = txtPassword.Text},
                                               ServiceCertificate =
                                                   {
                                                       Authentication =
                                                           {
                                                               CertificateValidationMode =
                                                                   X509CertificateValidationMode.None
                                                           }
                                                   }
                                           }
                                   };

                if (_service.ChangePassword(txtUserName.Text, txtPassword.Text, txtNewPassword.Text))
                {
                    pWord = txtNewPassword.Text;
                    var t = new Thread(ThreadProc);
                    t.Start();
                    Close();
                    _status.SUCCESS("Change Password");
                    return;
                }
                _status.FAIL("Change Password");
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void txtNewPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ChangPass();
            }
        }
    }
}